﻿using System;
using System.Collections.Generic;
using eShop.Business;
using eShop.Business;
using eShop.Model;
using Ext.Net;

public partial class admin_category_list : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;

        if (!X.IsAjaxRequest)
        {
            Session["_useSearch"] = null;
            LoadProductCategory();
        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindCategories(e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);
    }

    protected void imgSearchButton_Click(object sender, EventArgs e)
    {

    }

    private void BindCategories(string sort, int start, int limit)
    {
        List<Category> data = null;
        int totalCount = 0;

        if (this._useSearch)
        {
            string _searchCategoryName = txtToolbarSearch.Text;
            int _parentCategoryId;
            if (!Int32.TryParse(ddlToolbarCategories.SelectedItem.Value, out _parentCategoryId))
                _parentCategoryId = SiteUtility.SuperRootCategoryId;

            if (!string.IsNullOrEmpty(_searchCategoryName) && !(_parentCategoryId == SiteUtility.SuperRootCategoryId))
            {
                data = CategoryManager.GetBackendCategoryListByParentCategoryIdAndNameSearch(_parentCategoryId, _searchCategoryName, false, sort, start, limit);
                totalCount = CategoryManager.GetBackendCategoryListByParentCategoryIdAndNameCount(_parentCategoryId, _searchCategoryName, false);
            }
            else if (!string.IsNullOrEmpty(_searchCategoryName))
            {
                data = CategoryManager.GetBackendCategoryListByNameSearch(_searchCategoryName, false, sort, start, limit);
                totalCount = CategoryManager.GetBackendCategoryListByNameCount(_searchCategoryName, false);
            }
            else if (!(_parentCategoryId == SiteUtility.SuperRootCategoryId))
            {
                data = CategoryManager.GetBackendCategoryListByParentCategoryIdSearch(_parentCategoryId, false, sort, start, limit);
                totalCount = CategoryManager.GetBackendCategoryListByParentCategoryIdCount(_parentCategoryId, false);
            }
            else
            {
                data = CategoryManager.GetBackendCategoryList(false, sort, start, limit);
                totalCount = CategoryManager.GetBackendCategoryCount(false);
            }

            Session["_useSearch"] = null;
        }
        else
        {
            //data = ProductCategory.GetBackendCategoryList(false, this._sortBy, firstItem, pageSize);
            data = CategoryManager.GetBackendCategoryList(false, sort, start, limit);
            totalCount = CategoryManager.GetBackendCategoryCount(false);
        }

        Store1.DataSource = data;
        Store1.DataBind();

        (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }

    protected void LoadProductCategory()
    {
        //ddlParent.Items.Clear();
        List<Category> productCategoryTree = CategoryManager.GetRootCategories();
        foreach (Category productCategory in productCategoryTree)
        {
            ddlToolbarCategories.Items.Add(new Ext.Net.ListItem(productCategory.CategoryName, productCategory.Id.ToString()));
        }
    }

    
    [DirectMethod(Namespace = "EShop.WebService")]
    public void DeleteCategories(string ids)
    {
        ids = ids.TrimStart(' ');
        string[] idArray = ids.Split(' ');
        List<int> idList = new List<int>();
        foreach (string id in idArray) idList.Add(Convert.ToInt32(id));
        CategoryManager.DeleteCategories(idList);
        Store1.DataBind();
    }

    [DirectMethod(Namespace = "EShop.WebService")]
    public void SearchCategory()
    {
        _useSearch = true;
        Store1.DataBind();
    }
}